package com.jrx.anytxn.atom.service.impl;

import com.jrx.anytxn.atom.bean.CustBatchSaveBean;
import com.jrx.anytxn.atom.service.BaseBatchAccoutHandle;
import com.jrx.anytxn.common.exception.TxnException;
import com.jrx.anytxn.param.entity.PrOrganizationTable;
import com.jrx.anytxn.param.service.system.IOrganizationTableService;
import com.jrx.anytxn.transaction.bean.CastAccountBean;
import com.jrx.anytxn.transaction.bean.CastWaiveAccountBean;
import com.jrx.anytxn.transaction.constant.TransBizConstant;
import com.jrx.anytxn.transaction.entity.*;
import com.jrx.anytxn.transaction.service.IAccountService;
import org.apache.commons.collections.CollectionUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 客户批量-抛帐处理
 *
 * @program: anytxn-cf-parent
 * @description:
 * @author: xiaoyu.jing
 * @date: 2020-08-31 15:32
 **/
@Order(10)
@Service
public class CastAccountHandle extends BaseBatchAccoutHandle<CastAccountBean> {

    Logger logger = LoggerFactory.getLogger(CastAccountHandle.class);

    @Autowired
    private IOrganizationTableService organizationTableService;
    @Autowired
    private IAccountService accountService;

    /**
     * 抛帐业务逻辑处理
     *
     * @return
     * @throws TxnException
     */
    @Override
    public List<CastAccountBean> handBatchAccout() throws TxnException {
        CustBatchSaveBean custBatchSaveBean = CustBatchHelper.getCustBatchLocal();
        List<AmLoanAccountInfo> loanAcctList = custBatchSaveBean.getDealLoanAccountList();
        if (CollectionUtils.isEmpty(loanAcctList)) {
            return null;
        }

        List<AmLoanMainInfo> dealLoanMainList = custBatchSaveBean.getDealLoanMainList();
        List<AmRepaymentPlanInfo> dealPlanList = custBatchSaveBean.getDealPlanList();
        if(CollectionUtils.isEmpty(dealPlanList)){
            return null;
        }

        List<AmWaiveRepaymentPlanInfo> dealWaivePlanList = custBatchSaveBean.getDealWaivePlanList();
        List<AmAccountMainInfo> dealAcctList = custBatchSaveBean.getDealAcctList();
        List<CastAccountBean> castAccountBeanList = new ArrayList<>();
        for (AmLoanAccountInfo amLoanAccountInfo : loanAcctList) {
            String loanId = amLoanAccountInfo.getLoanId();
            //添加核销订单的判断 如果是核销的订单不做处理
            if (TransBizConstant.YES_FLAG.equals(amLoanAccountInfo.getChargeoffFlag())) {
                logger.info("订单已经核销无需处理。订单号：{}", loanId);
                continue;
            }

            PrOrganizationTable organization = organizationTableService.findByOrId(amLoanAccountInfo.getOrganizationId(), amLoanAccountInfo.getTenantId());

            //业务日期
            Date businessDate = organization.getToday();
            List<AmRepaymentPlanInfo> planList = dealPlanList.stream().filter(plan -> loanId.equals(plan.getLoanId())).collect(Collectors.toList());
            AmLoanMainInfo amLoanMainInfo = dealLoanMainList.stream().filter(loanMain -> loanId.equals(loanMain.getLoanId())).findFirst().get();
            List<AmWaiveRepaymentPlanInfo> waivePlanList = dealWaivePlanList.stream().filter(plan -> loanId.equals(plan.getLoanId())).collect(Collectors.toList());

            List<AmAccountMainInfo> castAcctList = dealAcctList.stream().filter(acct -> loanId.equals(acct.getLoanId()))
                    .filter(acct -> TransBizConstant.ACCOUNT_TYPE_06.equals(acct.getAccountType()))
                    .filter(acct -> amLoanAccountInfo.getCastTenor()+1 == (acct.getBillingTenor()))
                    .filter(acct -> businessDate.equals(acct.getPostingDate())).collect(Collectors.toList());
            if(CollectionUtils.isNotEmpty(castAcctList)){
                continue;
            }

            CastWaiveAccountBean castWaiveAccountBean = accountService.castTxnWaiveAccount(amLoanMainInfo, amLoanAccountInfo, waivePlanList, planList, businessDate, true);
            CastAccountBean castAccountBean = accountService.castTxnAccount(amLoanMainInfo, amLoanAccountInfo, planList, businessDate, true);
            if (null == castAccountBean) {
                castAccountBean = new CastAccountBean();
            }

            if (null == castWaiveAccountBean) {
                castAccountBeanList.add(castAccountBean);
                continue;
            }
            castAccountBean.addAccuIntrList(castWaiveAccountBean.getInterestAccrualInfoList());
            castAccountBean.setUpdateWaiveRepaymentPlanList(castWaiveAccountBean.getUpdateWaiveRepaymentPlanList());
            castAccountBeanList.add(castAccountBean);
        }
        logger.info("CastAccountHandle用户:{},需要处理的订单：{}",
                custBatchSaveBean.getDealCust().getCustomerId(),
                loanAcctList.stream().map(AmLoanAccountInfo::getLoanId).collect(Collectors.joining(","))
        );
        return castAccountBeanList;
    }
}
